package com.gy.api.pc.sys.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.*;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthoritySearchVO;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthorityTreeVO;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthorityVO;
import com.gy.api.pc.sys.authority.service.ISysAuthorityService;
import com.gy.api.pc.sys.role.domain.vo.SysRoleVO;
import com.gy.api.pc.sys.role.service.ISysRoleService;
import com.gy.api.pc.sys.user.domain.vo.*;
import com.gy.api.pc.sys.user.service.ISysUserService;
import com.gy.exception.BusinessException;
import com.gy.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysUserController)
@RequestMapping(value = "/sys/user")
@RestController
public class SysUserController {

    @Autowired
    private ISysUserService service;
    @Autowired
    private ISysAuthorityService sysAuthorityService;
    @Autowired
    private ISysRoleService roleService;

    @ApiOperation(value = "平台用户登录(账号密码都需要加密传入)")
    @PostMapping(value = "/login")
    @ResponseBody
    public R<UserLoginResVO> login(@ApiParam(value = "查询参数") @Valid @RequestBody UserLoginReqVO userLogin, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            UserLoginResVO userLoginResVO = service.login(userLogin);
            result.setData(userLoginResVO);
        }
        return result;
    }

    @ApiOperation(value = "查询平台用户列表")
    @GetMapping(value = "/list")
    @SaCheckLogin
    public R<RestPage<List<SysUserVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysUserSearchVO searchVO) {
        List<SysUserVO> list = service.getList(searchVO);
        list=service.getRole(list);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "获取当前登陆者信息")
    @GetMapping(value = "/one")
    @SaCheckLogin
    public R<SysUserOneVO> one() {
        SysUserOneVO one = service.getOne();

        return new R(one);
    }


    @ApiOperation(value = "校验TOKEN是否有效")
    @GetMapping(value = "/validToken")
    public R<Boolean> isToken(@ApiParam(value = "查询参数") @ModelAttribute TokenEntity token) {
        String logId= (String) StpUtil.getLoginIdByToken(token.getToken());
        if(StringUtils.isEmpty(logId)){
            throw new BusinessException(Type.FAIL.getCode(),"TOKEN无效");
        }
       // System.out.print(logId);
       // System.out.print(SaSameUtil.isValid(token.getToken()));
     //   SaSameUtil.checkToken(token.getToken());
      //  if(SaSameUtil.isValid(token.getToken())){
            return new R();
      //  }

    }


    @ApiOperation(value = "创建平台用户")
    @PostMapping(value = "/add")
    @SaCheckLogin
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysUserAddVO vo, BindingResult bindingResult) throws Exception {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除平台用户")
    @PostMapping(value = "/delete")
    @SaCheckLogin
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }

    @ApiOperation(value = "批量删除平台用户")
    @PostMapping(value = "/delete_batch")
    @SaCheckLogin
    public R deleteBath( @ApiParam(value = "修改参数") @Valid @RequestBody IDsEntity ids) {
        service.deleteBath(ids.getIds());
        return new R();
    }


    @ApiOperation(value = "禁言/启用-平台用户")
    @PostMapping(value = "/disableUser")
    @SaCheckLogin
    public R disableUser(@ApiParam(value = "参数")  @RequestBody SysUserStateVO vo) {
        service.disableUser(vo);
        return new R();
    }

    @ApiOperation(value = "修改平台用户")
    @PostMapping(value = "/update")
    @SaCheckLogin
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody SysUserUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }


    @ApiOperation(value = "重置自己的密码")
    @PostMapping(value = "/resetMyPassword")
    @SaCheckLogin
    public R resetMyPassword(@ApiParam(value = "用户ID") @Valid @RequestBody SysUserMyPasswordVO updateVO) {
        service.resetMyPassword(updateVO);
        return new R();
    }

    @ApiOperation(value = "重置平台用户密码")
    @PostMapping(value = "/resetPassword")
    @SaCheckLogin
    public R resetPassword(@ApiParam(value = "用户ID") @Valid @RequestBody SysUserPasswordVO updateVO) {
        service.resetPassword(updateVO);
        return new R();
    }


    @ApiOperation(value = "查询用户拥有权限列表")
    @GetMapping(value = "/authority_list")
    @SaCheckLogin
    public R<List<SysAuthorityVO>> getRoleAuthorityList(@ApiParam(value = "查询参数") @ModelAttribute SysAuthoritySearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getUserId())){
            searchVO.setUserId(StpUtil.getLoginId().toString());
        }
        List<SysAuthorityVO> list = sysAuthorityService.getList(searchVO);
        return new R(list);
    }


    @ApiOperation(value = "查询用户拥有权限(树形)")
    @GetMapping(value = "/authority_tree")
    @SaCheckLogin
    public R<List<SysAuthorityTreeVO>> authorityTree(@ApiParam(value = "查询参数") @ModelAttribute SysAuthoritySearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getUserId())){
            searchVO.setUserId(StpUtil.getLoginId().toString());
        }
        List<SysAuthorityTreeVO> list = sysAuthorityService.getTreeList(searchVO);
        return new R(list);
    }


    @ApiOperation(value = "查询所有角色列表")
    @GetMapping(value = "/role_list")
    @SaCheckLogin
    public R<List<SysRoleVO>> role_list() {
        List<SysRoleVO> list = roleService.getAllList();
        return new R(list);
    }
}
